//
//  UpLoadDesingInfoVC.m
//  BigJoy
//
//  Created by Rowling on 2017/7/7.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "UpLoadDesingInfoVC.h"
#import "PhotoCell.h"
#import "PersonalViewModel.h"
@interface UpLoadDesingInfoVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) PersonalViewModel *viewModel;
@property (weak, nonatomic) IBOutlet UIButton *designerImageButton;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnW;

@property (weak, nonatomic) IBOutlet UITextField *nameText;
@property (weak, nonatomic) IBOutlet UITextField *IDText;
@property (weak, nonatomic) IBOutlet UITextField *sexText;

@end

@implementation UpLoadDesingInfoVC

-(PersonalViewModel *)viewModel {
    if (_viewModel == nil) {
        _viewModel = [[PersonalViewModel alloc]init];
        [_viewModel bindView:self.view];
        @weakify(self);
        [_viewModel.choseImageSubject subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            if (self.viewModel.picCount == 8) {
                [self.baseMutableDataArray addObject:x];
                [self.collectionView reloadData];
            } else {
                [self.designerImageButton setImage:((UIImage *)x) forState:UIControlStateNormal];
                self.btnW.constant = 150;
                self.btnH.constant = 67;
                [self.designerImageButton layoutIfNeeded];
            }
            
        }];
    }
    return _viewModel;
}

-(UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(90, 90);
        layout.minimumInteritemSpacing = 15;
        layout.sectionInset = UIEdgeInsetsMake(0, 15, 5, 15);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([PhotoCell class]) bundle:nil] forCellWithReuseIdentifier:@"Cell"];
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"填写资料";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo(self.contentView);
    }];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.baseMutableDataArray.count + 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    PhotoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    if (indexPath.row == self.baseMutableDataArray.count) {
        cell.headImageView.image = [UIImage imageNamed:@"jiazhaop"];
        cell.closeButton.hidden = YES;
    }else {
        cell.headImageView.image = (UIImage *)self.baseMutableDataArray[indexPath.row];
        cell.closeButton.hidden = NO;
        cell.closeButton.tag = 100 + indexPath.row;
        [cell.closeButton addTarget:self action:@selector(closeButtonCLick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return cell;
}

-(void)closeButtonCLick:(UIButton *)sender {
    NSInteger tag = sender.tag - 100;
    [self.baseMutableDataArray removeObjectAtIndex:tag];
    [self.collectionView reloadData];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.baseMutableDataArray.count) {
        self.viewModel.picCount = 8;
        [self.viewModel showTheChosePhotoView];
    }
}

- (IBAction)imageButtonClick:(id)sender {
    self.viewModel.picCount = 1;
    [self.viewModel showTheChosePhotoView];
}
//性别
- (IBAction)secButtonClick:(id)sender {
    [self choseSex];
}
-(void)choseSex {
    choseTakePhotoVIew * choseView = [[choseTakePhotoVIew alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) titleArray:@[@"男",@"女"]];
    @weakify(self);
    [choseView.subject subscribeNext:^(id  _Nullable x) {
        long index = [x longValue];
        @strongify(self);
        if (index == 0) {
            self.sexText.text = @"男";
        }
        if (index == 1) {
            self.sexText.text = @"女";
        }
    }];
    [XMKEYWINDOWXM addSubview:choseView];
}
//提交审核
- (IBAction)upLoadInfo:(id)sender {
    [CustomAlerView showAlertMessage:@"资料提交成功，我们将在2-5个工作日内对您的资料进行审核"];
}

@end
